package thread.threadlocal;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 字符串转换成日期对象
 *
 * @author lwmzzz
 * @version 1.0
 * @date 2021/5/6 8:30
 */
public class Test02 {
    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");

    static ThreadLocal<SimpleDateFormat> threadLocal = new ThreadLocal<>();

    static class SubThreadLocal extends ThreadLocal<Date> {
        @Override
        protected Date initialValue() {
            return new Date(System.currentTimeMillis() - 1000 * 60 * 15);
        }
    }

    static class Covert2Date implements Runnable {
        private int i = 0;

        public Covert2Date(int i) {
            this.i = i;
        }

        @Override
        public void run() {
            String text = "2021年05月06日 08:34:" + i % 60;
//                Date date = sdf.parse(text);
            if (threadLocal.get() == null) {
                threadLocal.set(new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss"));
            }
            Date date = null;
            try {
                date = threadLocal.get().parse(text);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            System.out.println(i + "--" + date);
        }

    }


    public static void main(String[] args) {
        for (int i = 0; i < 100; i++) {
            new Thread(new Covert2Date(i)).start();
        }
    }
}
